﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o.Linq;
using BussinessEntity;
using Utilities;
using Db4objects.Db4o;

namespace DataAccessLayer
{
    public class ReceiptDA
    {
        public ActionResult<Receipt> Insert(Receipt entity)
        {
            ReceiptDetailDA adapter = new ReceiptDetailDA();
            ActionResult<Receipt> rs = new ActionResult<Receipt>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Receipt p in conn
                                where p.ReceiptID == entity.ReceiptID
                                select p).FirstOrDefault<Receipt>();
                    if (temp == null)
                    {
                        var tp = entity.ListDetails;
                        entity.ListDetails = null;
                        conn.Store(entity);
                        foreach (var l in tp)
                            l.Receipt = GetSingle(entity.ReceiptID,conn).Object;
                        adapter.Insert(tp,conn);
                        rs.Object = entity;
                    }
                    else
                        rs.AddException(new Exception("Trùng khóa chính"));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;

            }
        }

        public ActionResult<Receipt> Update(Receipt entity)
        {
            ActionResult<Receipt> rs = new ActionResult<Receipt>();
            ReceiptDetailDA adapter = new ReceiptDetailDA();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Receipt p in conn
                                where p.ReceiptID == entity.ReceiptID
                                select p).FirstOrDefault<Receipt>();
                    if (temp != null)
                    {
                        temp.CustomerName = entity.CustomerName;
                        temp.CustomerAddress = entity.CustomerAddress;
                        temp.DeviceName = entity.DeviceName;
                        temp.DeviceDescription = entity.DeviceDescription;
                        temp.DateReceiced = entity.DateReceiced;
                        temp.PhoneNumber = entity.PhoneNumber;
                        temp.Status = entity.Status;
                        adapter.Delete(entity.ReceiptID,conn);
                        adapter.Insert(entity.ListDetails,conn);
                        conn.Store(temp);
                        rs.Object = temp;
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + entity.ReceiptID + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Receipt> Delete(string MaHD)
        {
            ActionResult<Receipt> rs = new ActionResult<Receipt>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Receipt p in conn
                                where p.ReceiptID == MaHD
                                select p).FirstOrDefault<Receipt>();
                    if (temp != null)
                    {
                        ReceiptDetailDA adapter = new ReceiptDetailDA();
                        adapter.Delete(MaHD,conn);
                        conn.Delete(temp);
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + MaHD + " không tồn tại."));
                }
                catch (Exception exx)
                {
                    rs.AddException(exx);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Receipt> GetAll()
        {
            ActionResult<Receipt> rs = new ActionResult<Receipt>();
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    rs.Array = (from Receipt p in conn
                                select p).ToList<Receipt>();
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Receipt> GetSingle(string MaHD)
        {
            ActionResult<Receipt> rs = new ActionResult<Receipt>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Receipt p in conn
                                where p.ReceiptID == MaHD
                                select p).FirstOrDefault<Receipt>();
                    if (temp != null)
                    {
                        temp.ListDetails = (from ReceiptDetail p in conn
                                       where p.ReceiptID == MaHD
                                       select p).ToList<ReceiptDetail>();
                        foreach (var i in temp.ListDetails)
                        {
                            i.Service = (from Service p in conn
                                        where p.ServiceID == i.ServiceID
                                        select p).FirstOrDefault<Service>();
                            i.Receipt = (from Receipt p in conn
                                        where p.ReceiptID == i.ReceiptID
                                        select p).FirstOrDefault<Receipt>();
                        }
                        rs.Object = temp;
                    }
                    else
                        rs.AddException(new Exception("Mã hóa đơn: " + MaHD + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Receipt> GetSingle(string MaHD, IObjectContainer conn)
        {
            ActionResult<Receipt> rs = new ActionResult<Receipt>();
            try
            {
                var temp = (from Receipt p in conn
                            where p.ReceiptID == MaHD
                            select p).FirstOrDefault<Receipt>();
                if (temp != null)
                {
                    temp.ListDetails = (from ReceiptDetail p in conn
                                   where p.ReceiptID == MaHD
                                   select p).ToList<ReceiptDetail>();
                    foreach (var i in temp.ListDetails)
                    {
                        i.Service = (from Service p in conn
                                    where p.ServiceID == i.ServiceID
                                    select p).FirstOrDefault<Service>();
                        i.Receipt = (from Receipt p in conn
                                    where p.ReceiptID == i.ReceiptID
                                    select p).FirstOrDefault<Receipt>();
                    }
                    rs.Object = temp;
                }
                else
                    rs.AddException(new Exception("Mã hóa đơn: " + MaHD + " không tồn tại."));
            }
            catch (Exception ex)
            {
                rs.AddException(ex);
            }
            return rs;
        }

        public string NextID()
        {
            string nextid = "";
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    var temp = (from Receipt p in conn
                                orderby p.ReceiptID descending
                                select p).FirstOrDefault<Receipt>();
                    if (temp == null)
                        return "HĐ00001";
                    int ma = 0;
                    try
                    {
                        ma = DataUtil.ToInt(temp.ReceiptID.Substring(2)) + 1;
                    }
                    catch (Exception)
                    {
                        ma = 1;
                    }
                    nextid = "HĐ";
                    for (int i = 0; i < 5 - ma.ToString().Length; i++)
                        nextid += "0";
                    nextid += ma.ToString();
                    while (true)
                    {
                        temp = (from Receipt p in conn
                                where p.ReceiptID == nextid
                                select p).FirstOrDefault<Receipt>();
                        if (temp == null)
                            break;
                        ma++;
                        nextid = "HĐ";
                        for (int i = 0; i < 5 - ma.ToString().Length; i++)
                            nextid += "0";
                        nextid += ma.ToString();
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    conn.Close();
                }
            }
            return nextid;
        }
    }
}
